package org.glut.cloud.video.controller;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URL;
import java.util.Date;

@RestController
public class OssController {
    private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    private String accessKeyId = "LTAI5tGZs6cL1Soc4kW6n1EX";
    private String accessKeySecret = "OZN1EFYUp7DvmlOBRRnwhhbcJ2rdvb";
    private String bucketName = "video123-demo";


    @GetMapping("/api/oss/generatePresignedUrl")
    public String generatePresignedUrl(String objectName) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 设置URL过期时间为10分钟
        Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10);
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName);
        request.setExpiration(expiration);

        URL url = ossClient.generatePresignedUrl(request);
        ossClient.shutdown();

        return url.toString();
    }
}
